package com.flycms.module.solution.service;

import java.util.List;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.flycms.module.soft.dao.CaseDao;
import com.flycms.module.soft.dao.SoftDao;
import com.flycms.module.soft.model.Case;
import com.flycms.module.soft.model.Soft;
import com.flycms.module.solution.dao.SolutionDao;
import com.flycms.module.solution.dao.SolutionFunctionDao;
import com.flycms.module.solution.dao.SolutionPartnerDao;
import com.flycms.module.solution.model.Solution;

/**
 *
 * <br/>Copyright 2020
 * <br/>All rights reserved
 * @author 沁言訫語
 * @version 2020年3月10日
 * @Date 2020年3月10日
 */

@Service
public class SolutionService {

    @Autowired
    protected SolutionDao solutionDao;
    
    @Autowired
    protected SolutionPartnerDao solutionPartnerDao;
    
    @Autowired
    protected SolutionFunctionDao solutionFunctionDao;
    
    @Autowired
    protected SoftDao softDao;
    
    @Autowired
    protected CaseDao caseDao;
    
    @Transactional
    public int addSolution(Solution solution) throws Exception {
        return solutionDao.addSolution(solution);
    }
    
    @Transactional
    public int updateSolution(Solution solution) throws Exception {
        return solutionDao.updateSolution(solution);
    }
    
    @Transactional
    public int removeSolution(Solution solution) throws Exception {
        solutionFunctionDao.removeSolutionFunctionBySolutionId(solution.getId(), null);
        solutionPartnerDao.removeSolutionPartners(solution.getId(), null);
        solutionDao.removeSolutionCaseRel(solution.getId(), null);
        solutionDao.removeSolutionProductRel(solution.getId(), null);
        return solutionDao.removeSolution(solution);
    }
    
    public Solution querySolutionById(Integer id) throws Exception {
        return solutionDao.querySolutionById(id);
    }
    
    public List<Solution> getSolutionList(Integer categoryId, String title, int pageIndex,
            int pageSize) throws Exception {
        return solutionDao.getSolutionList(categoryId, title, pageIndex, pageSize);
    }
    
    public int getSolutionCount(Integer categoryId, String title) throws Exception {
        return solutionDao.getSolutionCount(categoryId, title);
    }
    
    @Transactional
    public int addSolutionCase(List<Integer> caseIds, Integer solutionId, Boolean single) throws Exception {
//        if (!Objects.equals(single, true)) {
//            solutionDao.removeSolutionCaseRel(solutionId, null);
//        }
        return solutionDao.addSolutionCaseRel(caseIds, solutionId);
    }
    
    @Transactional
    public int updateSolutionCase(List<Integer> caseIds, Integer solutionId) throws Exception {
        solutionDao.removeSolutionCaseRel(solutionId, null);
        return solutionDao.addSolutionCaseRel(caseIds, solutionId);
    }
    
    @Transactional
    public int removeSolutionCase(Integer solutionId, Integer caseId) throws Exception {
        return solutionDao.removeSolutionCaseRel(solutionId, caseId);
    }
    
    @Transactional
    public int addSolutionProduct(List<Integer> pIds, Integer solutionId, String scenarios, String advantage,
            Boolean single)
            throws Exception {
        if (scenarios != null || advantage != null) {
            Solution s = new Solution();
            s.setId(solutionId);
            s.setScenarios(scenarios);
            s.setAdvantage(advantage);
            solutionDao.updateSolution(s);
        }
//        if (!Objects.equals(single, true)) {
//            solutionDao.removeSolutionProductRel(solutionId, null);
//        }
        if (pIds.size() == 0) {
            return 1;
        }
        return solutionDao.addSolutionProductRel(pIds, solutionId);
    }
    
    @Transactional
    public int updateSolutionProduct(List<Integer> caseIds, Integer solutionId) throws Exception {
        solutionDao.removeSolutionProductRel(solutionId, null);
        return solutionDao.addSolutionProductRel(caseIds, solutionId);
    }
    
    @Transactional
    public int removeSolutionProduct(Integer solutionId, Integer productId) throws Exception {
        return solutionDao.removeSolutionProductRel(solutionId, productId);
    }
    
    public List<Soft> getSolutionProduct(Integer solutionId, Integer pageIndex, Integer pageSize) throws Exception {
        List<Soft> softList = solutionDao.getSolutionProductRel(solutionId, pageIndex, pageSize);
        return softList;
    }
    
    public int getSolutionProductCount(Integer solutionId) throws Exception {
        return solutionDao.getSolutionProductCount(solutionId);
    }
    
    public List<Case> getSolutionCase(Integer solutionId, Integer pageIndex, Integer pageSize) throws Exception {
        List<Case> caseList = solutionDao.getSolutionCaseRel(solutionId, pageIndex, pageSize);
        return caseList;
    }
    
    public int getSolutionCaseCount(Integer solutionId) throws Exception {
        return solutionDao.getSolutionCaseRelCount(solutionId);
    }
    
}
